package org.basic;

import io.netty.channel.DefaultEventLoop;
import io.netty.util.concurrent.DefaultPromise;

/**
 * @author wind
 * @create 2025-01-16 15:42
 */
public class EventLoopSync {
    public static void main(String[] args) throws Exception{
        DefaultEventLoop eventExecutors = new DefaultEventLoop();
        DefaultPromise<Integer> promise = new DefaultPromise<>(eventExecutors);

        eventExecutors.execute(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            RuntimeException e = new RuntimeException("error...");
            System.out.println("set failure, {} "+ e.toString());
            //log.error("set failure, {}", e.toString());
            promise.setFailure(e);
        });
        System.out.println("start...");
        System.out.println(promise.getNow());
        //log.debug("start...");
        //log.debug("{}", promise.getNow());
        promise.await(); // 与 sync 和 get 区别在于，不会抛异常
        System.out.println("result {}"+ (promise.isSuccess() ? promise.getNow() : promise.cause()).toString());
    }
}
